package com.sto.stoapp.thread;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.http.HttpManager;
import com.sto.stoapp.http.URLActions;
import com.sto.stoapp.http.request.upload.BarInfoRequest;
import com.sto.stoapp.http.response.upload.BarInfoResponse;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.Helper;

import android.content.Context;
import android.os.Handler;
//3.1.8.1 巴枪硬件信息采集
//000363{"cnt":"1","opRecord":[{"collectTime":"2015-03-24 18:46:05","deviceType":"PDA","memoryTotel":"120","memoryUsable":"60","memoryUsage":"60","onlineStatus":"1","opOrgCode":"200000","opTerminal":"520131425211111","power":"80","powerStatus":"0","useTime":"4800"}],"mty":"0854","sessionId":"3ce3e882706a4b3abcbd4837df9dc465","tid":"520131425211111","trace":"324000018"}
//收到{"respCode":"000","resMessage":"成功","sessionId":"9aec1ebf8da74df2b65f5bf8d862c82b","tid":"520131425211111","mty":"1854","trace":"325000018"}

public class BarInfoUploadThread extends BaseThread {
	Context context;
	Handler handler;

	public BarInfoUploadThread(Context context, Handler handler) {
		super(context);
		this.context = context;
		this.handler = handler;
	}

	@Override
	public void run() {
		super.run();
		String json = new BarInfoRequest(context).buildToRequestString();
		String response = "";
		try {

			HttpManager mgr = new HttpManager();
			response = mgr.doPostClient(json,
					StoFilePreference.getInstance(context).getServerIp()
							+ URLActions.HARDWARE_GATHER_ACTION);
			Gson gs = new Gson();
			BarInfoResponse barInfoResponse = gs.fromJson(response,
					new TypeToken<BarInfoResponse>() {
					}.getType());
			if ("000".equals(barInfoResponse.respCode)) {
				handler.post(new Runnable() {

					@Override
					public void run() {
						Helper.showToast(context, "硬件信息采集上传成功");
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
			ExceptionLog.writeLog("BarInfoUploadThread", e.toString());
			ExceptionLog.writeLog("BarInfoUploadThread", "发送:" + json);
			ExceptionLog.writeLog("BarInfoUploadThread", "收到:" + response);

		}

	}
}
